<template>
	<view class="mainbgc mainpadding">
		<view class="">
			<!-- 订单状态 -->
			<view class="bigtext" v-if="dataAll.state==1">等待买家付款</view>
			<view class="bigtext" v-if="dataAll.state==2">等待商家发货</view>
			<view class="bigtext" v-if="dataAll.state==3">等待买家确认收货</view>
			<view class="bigtext" v-if="dataAll.state==4">待评论</view>
			<view class="bigtext" v-if="dataAll.state==6">已完成</view>
			<view class="bigtext" v-if="dataAll.state==7">订单已取消</view>
		</view>
		<!--订单时间状态  -->
		<view class="margin_top1">
			<view class="strongtext" v-if="dataAll.state==1">剩余{{dataAll.itemend}}自动关闭</view>
			<view class="strongtext " v-if="dataAll.state==2">请您耐心等待商家发货~</view>
			<view class="strongtext " v-if="dataAll.state==3">订单将于{{dataAll.itemshend}}自动确认收货</view>
			<view class="strongtext" v-if="dataAll.state==4 || dataAll.state==5">感谢您的支持，欢迎下次购买！</view>
		</view>
		<!-- 地址 -->
		<view class="mainpadding ffffff radius margin_top">
			<view class="flexbetween">
				<view class="">
					<view class="flexleft">
						<view class="titletext margin_right3">{{dataAll.address.name}}</view>
						<view class="titletext">{{dataAll.address.mobile}}</view>
					</view>
					<view class="huisetext margin_top2">{{dataAll.address.address}}</view>
				</view>
				<!-- <view class="right_icon"></view> -->
			</view>
		</view>
		<!-- 商品 -->
		<view class="ffffff mainpadding radius margin_top">
			<view class="flexleft">
				<view class="titletext margin_right2">订单编号：{{dataAll.order_no}}</view>
			</view>
			<view class=" xiahuax margin_top" v-for="item in dataAll.goods" :key="item.id">
				<view class="flexbetween">
					<view class="shilitu">
						<image :src="item.image_text" mode=""></image>
					</view>
					<view class="" style="width: 74%;">
						<view class="titletext yhxiank">{{item.title}}</view>
						<view class="ershib margin_top1 yhxiank">{{item.difference}}</view>
						<view class="flexbetween margin_top1">
							<view class="qianshu">¥{{item.price}}</view>
							<view class="shuliang">x{{item.number}}</view>
						</view>
					</view>
				</view>
				<view class="flexright margin_top">
					<!--退货/售后-->
					<view class="huisebtn marginleft" v-if="item.refund_status==0&& dataAll.state==3"
						@click="returngoods(item)">退货/售后</view>
					<!-- 换货中 -->
					<view class="huangsebtn marginleft" v-if="item.refund_status==1">换货中</view>
					<!-- 待退货 -->
					<view class="huangsebtn marginleft" v-if="item.refund_status==2">待退货</view>
					<!-- 换货完成 -->
					<view class="huangsebtn marginleft" v-if="item.refund_status==3">换货完成</view>
					<!-- 换货被拒 -->
					<view class="huangsebtn marginleft" v-if="item.refund_status==5">换货被拒</view>
					<!-- 立即评价 -->
					<view class="huisebtn marginleft" @click="pingjia(item.id)"
						v-if="dataAll.state==4 && item.comment_status==1">立即评价</view>
				</view>
			</view>
		</view>
		<!-- 商品详情 -->
		<view class="paddingbom">
			<view class="ffffff mainpadding radius margin_top">
				<view class="flexbetween">
					<view class="ershil">商品金额</view>
					<view class="qianshu">¥{{dataAll.total_price}}</view>
				</view>
				<!-- <view class="flexbetween margin_top">
					<view class="ershil">运费</view>
					<view class="huisetext">¥111</view>
				</view> -->
				<view class="flexbetween margin_top">
					<view class="ershil">订单编号</view>
					<view class="huisetext">{{dataAll.order_no}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil">下单时间</view>
					<view class="huisetext">{{dataAll.createtime_text}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil">支付方式</view>
					<view class="huisetext">{{dataAll.pay_type_text}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil">备注</view>
					<view class="huisetext">{{dataAll.remarks}}</view>
				</view>
				<!-- <view class="flexbetween margin_top">
					<view class="ershil">退款金额</view>
					<view class="qianshu">11111111111111</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil">退货申请时间</view>
					<view class="huisetext">11111111111111</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil">退货原因</view>
					<view class="huisetext">11111111111111</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil">创建时间</view>
					<view class="huisetext">11111111111111</view>
				</view> -->
			</view>
		</view>
		<!-- 固定底部 -->
		<view class="gudingdb ffffff"
			v-if="dataAll.state==1 || dataAll.state==2 || dataAll.state==3|| dataAll.state==4 || dataAll.state==6 || dataAll.state==7">
			<view class="flexright">
				<view class="huisebtn margin_right" v-if="dataAll.state==1 || dataAll.state==2" @click="cancelorder">
					取消订单</view>
				<view class="huisebtn margin_right" v-if="dataAll.state==3" @click="showmodal=true">查看物流</view>
				<view class="huangsebtn" v-if="dataAll.state==1" @click="atoncepay">立即支付</view>
				<view class="huangsebtn" v-if="dataAll.state==3" @click="suresh">确认收货</view>
				<!-- <view class="huangsebtn" v-if="dataAll.state==4" @click="pingjia(listid)">立即评价</view> -->
				<view class="huisebtn margin_right3" v-if="dataAll.state==6 || dataAll.state==7" @click="delOrder">删除订单
				</view>
				<!-- <view class="huangsebtn" v-if="dataAll.state==6" @click="jumptkdetail">再次购买</view> -->
				<!-- <view class="huisebtn" v-if="dataAll.state==1">填写单号</view> -->
				<!-- <view class="huisebtn margin_right3" v-if="dataAll.state==1">退货/售后</view> -->
			</view>
		</view>
		<u-modal :show="showmodal" title="物流信息" confirmText="点击复制物流单号" @confirm="modalconfirm" confirmColor="#228dff"
			closeOnClickOverlay @close="showmodal=false">
			<view class="mainpadding">
				<view class="margin_top flexcenter strongtext">公司名称：{{dataAll.expressinfo.name}}</view>
				<view class="margin_top flexcenter strongtext">公司地址：{{dataAll.expressinfo.address}}</view>
				<view class="margin_top flexcenter strongtext">联系人：{{dataAll.expressinfo.lxrname}}</view>
				<view class="margin_top flexcenter strongtext">联系电话：{{dataAll.expressinfo.lxrmobile}}</view>
				<view class="margin_top flexcenter strongtext">物流单号：{{dataAll.express_no}}</view>
			</view>
		</u-modal>
		<!-- 取消订单原因列表 -->
		<quxiaolypop :messageInfo="{cancelshow:cancelshow,value:1}" @surecancel="surecancel"></quxiaolypop>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				dataAll: {},
				order_id: "",
				//取消订单开始
				qxly: {
					name: "",
					id: "",
				},
				//取消订单结束
				showmodal: false,
				cancelshow: false,
				listid: "",
			}
		},
		onLoad(options) {
			this.listid = options.id
		},
		onShow() {
			this.init()
		},
		methods: {
			// 确认收货
			suresh() {
				httpRequest.modal('提示', '请再次确认', true, (res) => {
					if (res) {
						httpRequest.request('/api/order/confirmOrder', 'POST', {
							id: this.listid,
						}).then(res => {
							httpRequest.toast(res.msg)
							this.init()
						})
					}
				})
			},
			// 删除订单
			delOrder() {
				httpRequest.modal('提示', '确定要删除当前订单吗？', true, (res) => {
					if (res) {
						httpRequest.request('/api/order/delOrder', 'POST', {
							id: this.listid,
						}).then(res => {
							httpRequest.toast(res.msg)
							if (res.code == 1) {
								setTimeout(() => {
									uni.navigateBack(1)
								}, 1000)
							}
						})
					}
				})
			},
			// 立即支付
			atoncepay(item) {
				let obj = {
					order_pay_no: this.dataAll.order_pay_no,
					pay_price: this.dataAll.pay_price,
				}
				uni.redirectTo({
					url: '/pages/shopping/querenqzf?msg=' + encodeURIComponent(JSON
						.stringify(obj))
				})
			},
			// 取消订单开始
			cancelorder(item) {
				this.order_id = this.listid
				this.cancelshow = true
			},
			surecancel(e) {
				this.qxly = e
				httpRequest.request('/api/order/cancelOrder', 'POST', {
					order_id: this.order_id,
					cancelyy_id: this.qxly.id,
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg);
					if (res.code == 1) {
						this.init()
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			// 取消订单结束
			init() {
				httpRequest.request('/api/order/getOrderInfo', 'POST', {
					id: this.listid,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						this.dataAll = res.data
						this.$set(this.dataAll, "itemend", "")
						this.$set(this.dataAll, "itemshend", "")
						this.dataAll.itemend = this.djstime(this.dataAll.expire_time * 1000)
						if (this.dataAll.auth_taketime) {
							this.dataAll.itemshend = this.djstime(this.dataAll.auth_taketime)
						}
						setInterval(() => {
							this.dataAll.itemend = this.djstime(this.dataAll.expire_time * 1000)
							if (this.dataAll.auth_taketime) {
								this.dataAll.itemshend = this.djstime(this.dataAll.auth_taketime)
							}
						}, 1000)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			djstime(endTime) {
				let startTime = (Date.parse(new Date())) / 1000;
				var timestamp = (Date.parse(new Date())) / 1000; //计算当前时间戳 (毫秒级)
				var date1 = ""; //开始时间
				if (timestamp < startTime) {
					date1 = startTime;
				} else {
					date1 = timestamp; //开始时间
				}
				var date2 = endTime; //结束时间
				// var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
				var date3 = (date2 - date1) * 1000; //时间差的毫秒数
				//计算出相差天数
				var days = Math.floor(date3 / (24 * 3600 * 1000));
				//计算出小时数
				var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
				var hours = Math.floor(leave1 / (3600 * 1000));
				//计算相差分钟数
				var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
				var minutes = Math.floor(leave2 / (60 * 1000));
				//计算相差秒数
				var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
				var seconds = Math.round(leave3 / 1000);
				// if (hours < 10) {
				// 	hours = "0" + hours
				// }
				// if (minutes < 10) {
				// 	minutes = "0" + minutes
				// }
				// if (seconds < 10) {
				// 	seconds = "0" + seconds
				// }
				/* days = Math.abs(days)
				hours = Math.abs(hours)
				minutes = Math.abs(minutes)
				seconds = Math.abs(seconds) */
				return days + "天" + hours + "小时" + minutes + "分钟"
			},
			// 跳转立即评价
			pingjia(id) {
				uni.navigateTo({
					url: '/pages/mypage/fabiaopinglun?id=' + id
				})
			},
			// 跳转退款详情界面
			jumptkdetail() {
				uni.navigateTo({
					url: '/pages/mypage/tuihuoxq'
				})
			},
			// 退货售后
			returngoods(item) {
				let obj = {
					order_goods_id: item.id,
					order_id: this.dataAll.id,
				}
				uni.navigateTo({
					url: "/pages/mypage/shenqingtk?msg=" + encodeURIComponent(JSON.stringify(obj))
				})
			},
			// 查看物流确认
			modalconfirm() {
				this.showmodal = false
				uni.setClipboardData({
					data: this.dataAll.express_no, //要被复制的内容
					success: () => { //复制成功的回调函数
						httpRequest.toast("复制成功")
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.huangsebtn {
		border: 2rpx solid rgba(34, 141, 255, 1);
		width: 170rpx;
		height: 67rpx;
		display: flex;
		border-radius: 120rpx;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: rgba(34, 141, 255, 1);
	}

	.huisebtn {
		border: 2rpx solid #666666;
		width: 170rpx;
		height: 67rpx;
		display: flex;
		border-radius: 120rpx;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.shuliang {
		font-size: 26rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #999999;
	}

	.ershib {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
	}

	.shilitu {
		width: 134rpx;
		height: 128rpx;
		border-radius: 14rpx;

		image {
			width: 100%;
			height: 100%;
			border-radius: 14rpx;
		}
	}

	.qianshu {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: RGBA(253, 70, 62, 1);
	}

	.huisetext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.yhxiank {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.paddingbom {
		padding-bottom: 200rpx;
	}
</style>